from app import db,login_manager
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime


class User(UserMixin,db.Model):
	__tablename__ = 'users'
	id=db.Column(db.Integer,primary_key=True)
	username = db.Column(db.String)
	real_name = db.Column(db.String)
	pwd_hash=db.Column(db.String)
	role_id = db.Column(db.Integer)
	location = db.Column(db.String(64))
	about_me = db.Column(db.Text())
	member_since = db.Column(db.DateTime(), default=datetime.utcnow)
	last_seen = db.Column(db.DateTime(), default=datetime.utcnow)
	posts = db.relationship('Plan', backref='creat_user', lazy='dynamic')
	
	def ping(self):
		self.last_seen = datetime.utcnow()
		db.session.add(self)
		db.session.commit()
	
	@property
	def password(self):
		raise AttributeError('password is not a readable attribute')
	
	@password.setter
	def password(self,input_pwd):
		self.pwd_hash=generate_password_hash(input_pwd)
		
	def verify_password(self,input_pwd):
		return check_password_hash(self.pwd_hash,input_pwd)
		
		
	def __repr__(self):
		return '<User %r>' % self.username
	
		
	
@login_manager.user_loader
def load_user(user_id):
	return User.query.get(int(user_id))



class Plan(db.Model):
	__tablename__ = 'plan'
	id = db.Column(db.Integer, primary_key=True)
	plan_name = db.Column(db.Text)
	plan_info = db.Column(db.Text)
	subject_id = db.Column(db.Integer)
	timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
	creat_uid = db.Column(db.Integer, db.ForeignKey('users.id'))
	status=db.Column(db.Integer)
	cases=db.relationship('TestCase',backref='base_plan', lazy='dynamic')

	
class TestCase(db.Model):
	__tablename__ = 'test_case'
	id = db.Column(db.Integer, primary_key=True)
	plan_id = db.Column(db.Integer, db.ForeignKey('plan.id'))
	host = db.Column(db.Text)
	url = db.Column(db.Text)
	method = db.Column(db.Text)
	parameter = db.Column(db.Text)
	expectation=db.Column(db.Text)
	depiction = db.Column(db.Text)
	update_time = db.Column(db.DateTime, default=datetime.utcnow)
